package org.saxonextensions;

import junit.framework.TestCase;
import net.sf.saxon.Configuration;
import net.sf.saxon.query.DynamicQueryContext;
import net.sf.saxon.query.StaticQueryContext;
import net.sf.saxon.query.XQueryExpression;

import javax.jcr.Node;
import javax.jcr.Session;
import javax.jcr.RepositoryException;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;

import org.saxonextensions.jcr.JCRDocumentWrapper;

/**
 * JCR Document wrapper test case
 *
 * @author Ivan Latysh
 * @since 16-Jan-2009 1:30:54 PM
 */
public class JCRDocumentWrapperUTest extends TestCase {

 public void testXPath() throws Exception {
   Session session = null; // Your JCR Session
   /** XQuery processor configuration */
   Configuration config = new Configuration();
   /** XQuery Static Context */
   StaticQueryContext staticEnv = new StaticQueryContext(config);
   // prepare result stream
   ByteArrayOutputStream xQueryResult = new ByteArrayOutputStream();
   Result reslt = new StreamResult(xQueryResult);
   // preset context node
   Node contextNode = session.getRootNode();
   // prepare new JCR Document
   JCRDocumentWrapper document = new JCRDocumentWrapper(contextNode, "jcr:root", config);
   // create a dynamic context
   DynamicQueryContext dynamicEnv = new DynamicQueryContext(config);
   // set context item
   dynamicEnv.setContextItem(document);
   // compile expression
   XQueryExpression expression = staticEnv.compileQuery("<names>{for $v in * return <name>{$v/@name}</name>}</names>");
   // execute expression
   expression.run(dynamicEnv, reslt, null);
   // output response data
   System.out.println(xQueryResult.toString());
 }

}
